package com.neusoft.test;

import com.alibaba.druid.pool.DruidDataSource;
import com.neusoft.dao.StudentDao;
import com.neusoft.dao.UmsAdminDao;
import com.neusoft.model.Student;
import com.neusoft.service.StudentServiceImpl;
import com.neusoft.service.UmsAdminService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Author: duan
 * @Date: Create in 16:52 2020/10/26
 */
public class SpringDemo {
    @Test
    public void t1() {
        //ClassPath指定的是啥？指是的编译后的classes目录
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从Spring容器中获取对象
        StudentDao studentDao = (StudentDao) context.getBean("studentDao");
        StudentDao studentDao1 = (StudentDao) context.getBean("studentDao");
        System.out.println(studentDao);
        System.out.println(studentDao1);
        System.out.println(studentDao == studentDao1);
        StudentDao studentDao2 = (StudentDao) context.getBean("sDao");
        StudentDao studentDao3 = (StudentDao) context.getBean("stuDao");
        System.out.println(studentDao2);
        System.out.println(studentDao3);
        ClassPathXmlApplicationContext cp = (ClassPathXmlApplicationContext) context;
        cp.close();
    }

    @Test
    public void t2() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从Spring容器中获取对象
        StudentDao studentDao = (StudentDao) context.getBean("studentDao");
        System.out.println(studentDao);
        ClassPathXmlApplicationContext cp = (ClassPathXmlApplicationContext) context;
        cp.close();
    }

    @Test
    public void t3() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从Spring容器中获取对象
        StudentServiceImpl studentService = (StudentServiceImpl) context.getBean("studentService");
        System.out.println(studentService);
        StudentDao studentDao = (StudentDao) context.getBean("studentDao");
        studentService.setStudentDao(studentDao);
        studentService.save();
        ClassPathXmlApplicationContext cp = (ClassPathXmlApplicationContext) context;
        cp.close();
    }

    @Test
    public void t4() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从Spring容器中获取对象
        StudentServiceImpl studentService = (StudentServiceImpl) context.getBean("studentService");
        System.out.println(studentService);
        StudentDao studentDao = (StudentDao) context.getBean("studentDao");
        studentService.setStudentDao(studentDao);
        studentService.save();
        ClassPathXmlApplicationContext cp = (ClassPathXmlApplicationContext) context;
        cp.close();
    }

    @Test
    public void t5() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从Spring容器中获取对象
        StudentServiceImpl studentService = (StudentServiceImpl) context.getBean("studentService");
        System.out.println(studentService);
        StudentServiceImpl stuService = (StudentServiceImpl) context.getBean("stuService");
        System.out.println(stuService);
        StudentServiceImpl sService = (StudentServiceImpl) context.getBean("sService");
        System.out.println(sService);
        studentService.save();
        stuService.save();
        sService.save();
        ClassPathXmlApplicationContext cp = (ClassPathXmlApplicationContext) context;
        cp.close();
    }

    @Test
    public void t6() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从Spring容器中获取对象
        Student studentService = context.getBean(Student.class);
        System.out.println(studentService);
        ClassPathXmlApplicationContext cp = (ClassPathXmlApplicationContext) context;
        cp.close();
    }

    @Test
    public void t7() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从Spring容器中获取对象
        StudentDao studentDao = (StudentDao) context.getBean("ssDao");
        System.out.println(studentDao);
        ClassPathXmlApplicationContext cp = (ClassPathXmlApplicationContext) context;
        cp.close();
    }

    @Test
    public void t8() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从Spring容器中获取对象
        DruidDataSource dataSource = (DruidDataSource) context.getBean("dataSource");
        System.out.println(dataSource);
        ClassPathXmlApplicationContext cp = (ClassPathXmlApplicationContext) context;
        cp.close();
    }

    @Test
    public void t9() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从Spring容器中获取对象
        UmsAdminDao umsAdminDao = (UmsAdminDao) context.getBean("umsAdminDao");
        System.out.println(umsAdminDao);
        System.out.println(umsAdminDao.queryById(3L));
        UmsAdminService umsAdminService = context.getBean("umsAdminService", UmsAdminService.class);
        System.out.println(umsAdminService);

    }
}
